package cn.lili.listener;

import cn.lili.modules.system.client.RequestLogClient;
import cn.lili.modules.system.client.ResponseLogClient;
import cn.lili.modules.system.entity.dos.RequestLog;
import cn.lili.modules.system.entity.dos.ResponseLog;
import cn.lili.routing.ThirdCallLogKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 第三方调用日志监听
 *
 * @author pyy
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RequestLogListener {

    @Autowired
    private RequestLogClient requestLogClient;
    @Autowired
    private ResponseLogClient responseLogClient;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = ("${lili.amqp.msg}" + "_" + ThirdCallLogKey.REQUEST_LOG)),
            exchange = @Exchange(value = "${lili.amqp.msg}"),
            key = ThirdCallLogKey.REQUEST_LOG))
    public void onMessage(RequestLog requestLog) {
        requestLogClient.saveOrUpdate(requestLog);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = ("${lili.amqp.msg}" + "_" + ThirdCallLogKey.RESPONSE_LOG)),
            exchange = @Exchange(value = "${lili.amqp.msg}"),
            key = ThirdCallLogKey.RESPONSE_LOG))
    public void onMessage(ResponseLog responseLog) {
        responseLogClient.saveOrUpdate(responseLog);
    }

}
